<?php
declare (strict_types = 1);

namespace app\adminapi\controller\user;

use app\adminapi\validate\UserValidate;
use app\common\controller\BaseController;
use app\common\service\user\UserService;
use think\Request;

class User extends BaseController
{
    public function __construct(UserService $service)
    {
        $this->service = $service;
    }

    /**
     * 获取用户列表
     *
     * @param Request $request
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function index(Request $request): \think\Response
    {
        $where = $this->getParams();
        return $this->success($this->service->getList($where));
    }

    /**
     * 创建一条记录
     *
     * @param UserValidate $validate
     * @return \think\Response
     * @throws \Exception
     */
    public function save(UserValidate $validate): \think\Response
    {
        $data = $this->getParams();
        $result = $validate->scene('add')->check($data);
        if(!$result){
            return $this->notValidate($validate->getError());
        }
        return $this->success($this->service->create($data));
    }

    /**
     * 查询一条记录
     *
     * @param $id
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function read($id): \think\Response
    {
        return $this->success($this->service->getInfo($id));
    }

    /**
     * 更新一条记录
     *
     * @param $id
     * @param UserValidate $validate
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update($id, UserValidate $validate): \think\Response
    {
        $data = $this->getParams();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            return $this->notValidate($validate->getError());
        }
        return $this->success($this->service->update($data, $id));
    }

    /**
     * 删除一条记录
     *
     * @param $id
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($id): \think\Response
    {
        return $this->success($this->service->delete($id));
    }

    /**
     * 拉黑用户
     *
     * @param $id
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function blockUser($id): \think\Response
    {
        $data = $this->getParams();
        return $this->success($this->service->update($data, $id));
    }

    /**
     * 修改用户金币
     *
     * @param $id
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function saveUserBalance($id): \think\Response
    {
        $data = $this->getParams();
        return $this->success($this->service->saveUserBalance($id, $data));
    }
}
